const Router = require("koa-router");
const { Flow } = require("../../models/flow");
const { Favor } = require("@models/favor");
const { PositiveIntegerValidator, ClassicValidator } = require("@validator");
const router = new Router({
	prefix: "/v1/classic"
});
const { Auth } = require("../../../middlewares/auth");
const { Art } = require("../../models/art");


router.get('/latest', new Auth().m, async(ctx, next) => {

    const flow = await Flow.findOne({
			order: [["index", "DESC"]]
		});
		const art = await Art.getData(flow.art_id, flow.type);
		const i = art.get("image");
		const t = art.image;
		const s = art.getDataValue("image");
		const likeLatest = await Favor.userLikeIt(
			flow.art_id,
			flow.type,
			ctx.auth.uid
		);
		art.setDataValue("index", flow.index);
		art.setDataValue("like_status", likeLatest);
		ctx.body = art;
})

// model code first, 表， model
// 面向对象 model Class
// 初始化数据 期刊 数据，SQL
// model
// movie/Sentence/music 扩展性 相似性
// url pubdate title 导演 演员
router.get('/:index/next', new Auth().m, async (ctx) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'index'
    })
    const index = v.get('path.index')
    const flow = await Flow.findOne({
        where: {
            index: index + 1
        }
    })
    if (!flow) {
        throw new global.errs.NotFound()
    }
    const art = await Art.getData(flow.art_id, flow.type)
    const likeNext = await Favor.userLikeIt(
        flow.art_id, flow.type, ctx.auth.uid)
    art.setDataValue('index', flow.index)
    art.setDataValue('like_status', likeNext)
    // art.exclude = ['index','like_status']
    ctx.body = art
})

router.get('/:index/previous', new Auth().m, async (ctx) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'index'
    })
    const index = v.get('path.index')
    const flow = await Flow.findOne({
        where: {
            index: index - 1
        }
    })
    if (!flow) {
        throw new global.errs.NotFound()
    }
    const art = await Art.getData(flow.art_id, flow.type)
    const likePrevious = await Favor.userLikeIt(
        flow.art_id, flow.type, ctx.auth.uid)
    art.setDataValue('index', flow.index)
    art.setDataValue('like_status', likePrevious)
    ctx.body = art
})


router.get('/:type/:id', new Auth().m, async ctx => {
    const v = await new ClassicValidator().validate(ctx)
    const id = v.get('path.id')
    const type = parseInt(v.get('path.type'))

    const artDetail = await new Art(id, type).getDetail(ctx.auth.uid)

    artDetail.art.setDataValue('like_status', artDetail.like_status)
    ctx.body = artDetail.art
})

router.get('/:type/:id/favor', new Auth().m, async ctx => {
    const v = await new ClassicValidator().validate(ctx)
    const id = v.get('path.id')
    const type = parseInt(v.get('path.type'))

    const artDetail = await new Art(id, type).getDetail(ctx.auth.uid)

    ctx.body = {
        fav_nums: artDetail.art.fav_nums,
        like_status: artDetail.like_status
    }
})

router.get('/favor', new Auth().m, async ctx => {
    const uid = ctx.auth.uid
    ctx.body = await Favor.getMyClassicFavors(uid)
})


module.exports = router;